package com.fy56.platform.logistics.service.impl;

import com.fy56.platform.logistics.common.ResultModel;
import com.fy56.platform.logistics.common.ResultStatus;
import com.fy56.platform.logistics.mapper.UserCompanyMapper;
import com.fy56.platform.logistics.mapper.VipAppIdMapper;
import com.fy56.platform.logistics.mapper.VipMapper;
import com.fy56.platform.logistics.mapper.VipOpenidRelateMapper;
import com.fy56.platform.logistics.model.*;
import com.fy56.platform.logistics.service.wx.IVipService;
import com.fy56.platform.logistics.utils.MD5Util;
import com.fy56.platform.logistics.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


/**
 * VIP用户Service
 * Created by li on 2018-01-29.
 */
@Service
public class VipServiceImpl implements IVipService {

    @Autowired
    private VipMapper vipMapper;

    @Autowired
    private UserCompanyMapper userCompanyMapper;

    @Autowired
    private VipOpenidRelateMapper vipOpenidRelateMapper;

    @Autowired
    private VipAppIdMapper vipAppIdMapper;

    @Override
    @Transactional(readOnly = true)
    public boolean existByMobile(String mobile){
        Vip vip = new Vip();
        vip.setMobile(mobile);
        List<Vip> list = select(vip);
        if(list == null || list.size() == 0){
            return false;
        }
        return true;
    }

    @Override
    @Transactional(readOnly = true)
    public Vip selectVipOpenidByMobile(String mobile){
        Vip resultMap = null;
        Vip vip = new Vip();
        vip.setMobile(mobile);
        List<Vip> tempList = vipMapper.selectVipOpenidByMobile(vip);
        if(tempList != null && tempList.size() > 0){
            resultMap = tempList.get(0);
        }
        return resultMap;
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel updateFormIdsByMobile(Vip entity) {
        ResultModel result = null;
        int r = vipMapper.updateFormIdsByMobile(entity);
        if (r == 0) {
            result = new ResultModel(ResultStatus.EDIT_ERR);
        }else {
            result = ResultModel.ok();
        }
        return result;
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel updateVipOpenidRelate(VipOpenidRelate entity) {
        ResultModel result = null;
        int r = vipOpenidRelateMapper.updateVipOpenidRelate(entity);
        if (r == 0) {
            result = new ResultModel(ResultStatus.EDIT_ERR);
        }else {
            result = ResultModel.ok();
        }
        return result;
    }


    @Override
    @Transactional(readOnly = true)
    public boolean existByVip(VipOpenidRelate entity){
        List<VipOpenidRelate> list = vipOpenidRelateMapper.select(entity);
        if(list == null || list.size() == 0){
            return false;
        }
        return true;
    }

    @Override
    @Transactional(readOnly = true)
    public List<Vip> select(Vip entity) {
        return vipMapper.select(entity);
    }

    @Override
    @Transactional(readOnly = true)
    public List<VipOpenidRelate> selectVipOpenidRelate(VipOpenidRelate vipOpenidRelate) {
        return vipOpenidRelateMapper.selectVipOpenidRelate(vipOpenidRelate);
    }

    @Override
    @Transactional(readOnly = true)
    public Vip selectByOpenId(String openid) {
        VipOpenidRelate model = vipOpenidRelateMapper.selectByOpenid(openid);
        if(model != null){
            Vip test=null;
            try{
                 test =vipMapper.selectByPrimaryKey(model.getVipMemberId());
            }catch (Exception e){
                System.out.println(e.getMessage());
                String a="111";

            }

            return test ;
        }
        return null;
        //return vipMapper.selectByOpenId(openid);
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel updatePassByMobile(Vip entity) {
        ResultModel result = null;
        Vip dbModel = vipMapper.selectByMobile(entity);
        if(dbModel != null){
            entity.setId(dbModel.getId());
            entity.setUpdateTime(new Date());
            entity.setPassword(MD5Util.getMD5Code(entity.getPassword()).toUpperCase());
            int r = vipMapper.updateByPrimaryKeySelective(entity);
            if (r == 0) {
                result = new ResultModel(ResultStatus.EDIT_ERR);
            }else {
                result = ResultModel.ok(dbModel);
            }
        }else{
            result = new ResultModel("用户[" + entity.getId() + "]已被删除");
        }
        return result;
    }


    @Override
    @Transactional(readOnly = false)
    public ResultModel saveVipOpenid(VipOpenidRelate entity) {
        ResultModel result = null;
        int r = vipOpenidRelateMapper.insert(entity);
        if (r == 0) {
            result = new ResultModel(ResultStatus.EDIT_ERR);
        }else {
            result = ResultModel.ok(entity);
        }
        return result;
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel saveVipAppId(VipAppId entity) {
        ResultModel result = null;
        try{

            VipAppId model = vipAppIdMapper.selectByOpenid(entity.getVipMemberId());
            int r = vipAppIdMapper.insert(entity);
            if (r == 0) {
                result = new ResultModel(ResultStatus.EDIT_ERR);
            }else {
                result = ResultModel.ok(entity);
            }
        }catch (Exception e){
            System.out.println(e);
        }

        return result;
    }
    @Override
    @Transactional(readOnly = false)
    public ResultModel updateVipAppId(VipAppId entity) {
        ResultModel result = null;
        try{
            int r = vipAppIdMapper.updateByPrimaryKeySelective(entity);
            if (r == 0) {
                result = new ResultModel(ResultStatus.EDIT_ERR);
            }else {
                result = ResultModel.ok(entity);
            }
        }catch (Exception e){
            System.out.println(e);
        }

        return result;
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel removeOpenid(VipOpenidRelate entity) {
        ResultModel result = null;
        int r = vipOpenidRelateMapper.deleteByVipOpenidRelate(entity);
        //int r = vipMapper.removeOpenid(entity);
        if (r == 0) {
            result = new ResultModel(ResultStatus.EDIT_ERR);
        }else {
            result = ResultModel.ok(entity);
        }
        return result;
    }

    @Override
    @Transactional(readOnly = true)
    public Vip selectByPrimaryKey(Long id) {
        return vipMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional(readOnly = true)
    public Vip get(Vip entity) {
        List<Vip> userList = vipMapper.select(entity);
        if(userList != null && userList.size() > 0){
            return userList.get(0);
        }
        return null;
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel insert(Vip entity) {
        ResultModel result = null;
        int r = vipMapper.insert(entity);
        if(r == 0) {
            result = ResultModel.error(ResultStatus.INSERT_ERR);
        }else {
            result = ResultModel.ok();
        }
        return result;
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel insertSelective(Vip entity) {
        return null;
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel update(Vip entity) {
        ResultModel result = null;
        Vip dbModel = vipMapper.selectByPrimaryKey(entity.getId());
        if(dbModel != null){
            dbModel.setName(entity.getName());
            dbModel.setIdNo(entity.getIdNo());
            dbModel.setUpdateTime(new Date());

            //实名制
            if(StringUtil.isNotEmpty(entity.getIdFrontPic())){
                dbModel.setAuditStatus(0);
                dbModel.setIdFrontPic(entity.getIdFrontPic());
                dbModel.setIdBackPic(entity.getIdBackPic());
                dbModel.setIdHandPic(entity.getIdHandPic());
            }else{

                //修改个人信息
                dbModel.setName(entity.getName());
                dbModel.setAddress(entity.getAddress());
                dbModel.setIdNo(entity.getIdNo());
                dbModel.setPhone(entity.getPhone());
                dbModel.setCompany(entity.getCompany());
                dbModel.setProvince(entity.getProvince());
                dbModel.setCity(entity.getCity());
                dbModel.setAreaCode(entity.getAreaCode());
            }

            //修改vip
            int r = vipMapper.updateByPrimaryKey(dbModel);

            //修改默认物流公司
            UserCompany model = new UserCompany();
            model.setIsDefault(1);
            model.setUserId(entity.getId());
            List<UserCompany> list = userCompanyMapper.select(model);
            if(list != null && list.size() > 0){
                model = list.get(0);
                if(!model.getCompanyId().equals(entity.getDefaultTenant())){
                    model.setCompanyId(Long.valueOf(entity.getDefaultTenant()));
                    userCompanyMapper.update(model);
                }
            }

            if (r == 0) {
                result = new ResultModel(ResultStatus.EDIT_ERR);
            }else {
                result = ResultModel.ok(dbModel);
            }
        }else{
            result = new ResultModel("用户[" + entity.getId() + "]已被删除");
        }
        return result;
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel updateSelective(Vip entity) {
        ResultModel result = null;
        int r = vipMapper.updateByPrimaryKeySelective(entity);
        if (r == 0) {
            result = new ResultModel(ResultStatus.EDIT_ERR);
        }else {
            result = ResultModel.ok();
        }
        return result;
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel delete(Long id) {
        int r = vipMapper.deleteByPrimaryKey(id);
        if (r == 0){
            return new ResultModel(ResultStatus.DELETE_ERR);
        }
        return ResultModel.ok();
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel delete(Vip entity) {
        int r = vipMapper.updateByPrimaryKeySelective(entity);
        if (r == 0){
            return new ResultModel(ResultStatus.DELETE_ERR);
        }
        return ResultModel.ok();
    }

    @Override
    @Transactional(readOnly = true)
    public List<Vip> findList(Vip entity) {
        List<Vip> list = vipMapper.findList(entity);
        return list;
    }

    @Override
    @Transactional(readOnly = true)
    public Vip getVipById(Long id){
        return vipMapper.selectById(id);
    }

    @Override
    @Transactional(readOnly = true)
    public VipAppId getVipAppIdById(VipAppId vipAppId){
        return vipAppIdMapper.selectVipAppidByCode(vipAppId);
    }
    @Override
    @Transactional(readOnly = true)
    public ResultModel updateAuditStatus(Vip entity){
        ResultModel result = null;
        int r = vipMapper.updateAuditStatus(entity);
        if (r == 0) {
            result = new ResultModel(ResultStatus.EDIT_ERR);
        }else {
            result = ResultModel.ok(entity);
        }
        return result;
    }

    @Override
    @Transactional(readOnly = false)
    public  ResultModel batchAuditStatusPass(ArrayList<Vip> list){
        LinkedList<String[]> failedIds = new LinkedList<String[]>();
        int r = 0;
        for (Vip model : list) {
            r = vipMapper.updateAuditStatus(model);
            if (r == 0) {
                failedIds.add(new String[]{model.getId().toString(), "批量审核失败!"});
            }
        }
        if(failedIds.size() > 0){
            return new ResultModel(0, "", failedIds);
        }else{
            return ResultModel.ok();
        }
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel updateDefaultPlaceOfLoading(Vip entity){
        ResultModel result = null;
        int r = vipMapper.updateDefaultPlaceOfLoading(entity);
        if (r == 0) {
            result = new ResultModel(ResultStatus.EDIT_ERR);
        }else {
            result = ResultModel.ok(entity);
        }
        return result;
    }

}
